package com.clarence.dada.modular.system.menu.enums;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.EnumValue;
import com.clarence.dada.core.base.ReadableEnum;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;

/**
 * 默认菜单按钮枚举
 * <p>
 * 一般用来表示开启和关闭
 *
 * @author GD
 * @since 2020/10/14 21:31
 */
@Getter
public enum MenuButtonEnum implements ReadableEnum<MenuButtonEnum> {

    /**
     * 添加按钮
     */
    DEFAULT_ADD_BUTTON("ADD_BUTTON", "新增"),

    /**
     * 删除按钮
     */
    DEFAULT_DEL_BUTTON("DEL_BUTTON", "删除"),

    /**
     * 修改按钮
     */
    DEFAULT_UPDATE_BUTTON("UPDATE_BUTTON", "修改"),

    /**
     * 查询按钮
     */
    DEFAULT_SEARCH_BUTTON("SEARCH_BUTTON", "查询");

    @EnumValue
    @JsonValue
    private final String code;

    private final String message;

    MenuButtonEnum(String code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 根据code获取枚举
     *
     * @author GD
     * @since 2020/10/29 18:59
     */
    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static MenuButtonEnum codeToEnum(String code) {
        if (null != code) {
            for (MenuButtonEnum item : MenuButtonEnum.values()) {
                if (item.getCode().equals(code)) {
                    return item;
                }
            }
        }
        return null;
    }

    @Override
    public Object getKey() {
        return this.code;
    }

    @Override
    public Object getVal() {
        return this.message;
    }

    @Override
    public MenuButtonEnum parseToEnum(String originValue) {
        if (ObjectUtil.isEmpty(originValue)) {
            return null;
        }
        for (MenuButtonEnum value : MenuButtonEnum.values()) {
            if (value.code.equals(originValue)) {
                return value;
            }
        }
        return null;
    }
}
